package com.tjp.eight.views;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tjp.eight.R;

/**
 * Created by Administrator on 2017/3/27.
 */

public class CustomDialog extends Dialog {
    private Context mContext;
    private LayoutInflater inflater;
    private WindowManager.LayoutParams lp;
    private TextView msgTv;
    private Button buttonOK;
    private Button buttonCancel;
    private TextView mTitle;

    public CustomDialog(Context context) {
        super(context, R.style.Dialog);

        this.mContext = context;

        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.dailog_content_overlay, null);
        msgTv = (TextView) layout.findViewById(R.id.CustomDlgContentText);
        buttonOK = (Button) layout.findViewById(R.id.CustomDlgButtonOK);
        buttonCancel = (Button) layout.findViewById(R.id.CustomDlgButtonCancel);
        mTitle = (TextView) layout.findViewById(R.id.CustomDlgTitle);

        setContentView(layout);

        // 设置window属性
        lp = getWindow().getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.dimAmount = 0; // 去背景遮盖
        lp.alpha = 1.0f;
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(true);
    }

    public void dismiss() {
        super.dismiss();
    }

    //设置Dailog的标题
    public void setTitle(String title) {
        mTitle.setText(title);
    }

    //设置Dailog的主体内容
    public void setMessage(String message) {
        msgTv.setText(message);
    }

    //设置Dailog的“确定”按钮
    public void setPositiveButton(String text, View.OnClickListener listener) {
        if (TextUtils.isEmpty(text)) {
            text = "是";
        }
        buttonOK.setText(text);
        buttonOK.setOnClickListener(listener);
        buttonOK.setVisibility(View.VISIBLE);
    }

    /**
     * 设置Dailog的“确定”按钮隐藏
     */
    public void SetPositiveGone() {
        buttonOK.setVisibility(View.GONE);
    }

    public void setPositiveButtonParams(LinearLayout.LayoutParams params) {
        buttonOK.setLayoutParams(params);
    }

    //设置Dailog的“取消”按钮
    public void setNegativeButton(String text, View.OnClickListener listener) {
        if (TextUtils.isEmpty(text)) {
            text = "否";
        }
        buttonCancel.setText(text);
        buttonCancel.setOnClickListener(listener);
        buttonCancel.setVisibility(View.VISIBLE);
    }
}
